//
//  FindController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "FindController.h"
#import "ZTRecommendViewController.h"
#import "ZTListViewController.h"
#import "ZTAnchorViewController.h"


#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface FindController () <UIScrollViewDelegate>

@property (nonatomic, strong) ZTRecommendViewController *recommendVC;

@property (nonatomic, strong) ZTListViewController *listVC;

@property (nonatomic, strong) ZTAnchorViewController *anchorVC;

@property (nonatomic, strong) UIScrollView *bigScrollView;

@property (nonatomic, strong) UIButton *recommendBtn;

@property (nonatomic, strong) UIButton *listBtn;

@property (nonatomic, strong) UIButton *anchorBtn;

@property(nonatomic,retain)UIView *sliderView;

@end

@implementation FindController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"发现";
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};

    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    
#pragma mark - 创建按钮
    //推荐按钮
    self.recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recommendBtn.frame = CGRectMake(0, 0, WIDTH / 3, 40);
    [self.recommendBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
    [self.recommendBtn setTitle:@"推荐" forState:UIControlStateNormal];
    self.recommendBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.recommendBtn];
    [self.recommendBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //榜单按钮
    self.listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.listBtn.frame = CGRectMake(self.recommendBtn.frame.origin.x+self.recommendBtn.frame.size.width * 1, 0, WIDTH / 3, self.recommendBtn.frame.size.height);
    [self.listBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
    [self.listBtn setTitle:@"榜单" forState:UIControlStateNormal];
    self.listBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.listBtn];
    [self.listBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //主播按钮
    self.anchorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.anchorBtn.frame = CGRectMake(self.recommendBtn.frame.origin.x+self.recommendBtn.frame.size.width * 2, 0, WIDTH / 3, self.recommendBtn.frame.size.height);
    [self.anchorBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
    [self.anchorBtn setTitle:@"主播" forState:UIControlStateNormal];
    self.anchorBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.anchorBtn];
    [self.anchorBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    
#pragma mark - 创建ScrollView
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT)];
    self.bigScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bigScrollView];
    // 横滚
    self.bigScrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    // 设置按页滑动(相册刹车)
    self.bigScrollView.pagingEnabled = YES;
    // 是否边界反弹
    self.bigScrollView.bounces = NO;
    // 是否显示滑动条(水平)
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    self.bigScrollView.delegate = self;
    
    
    
    
#pragma mark - 创建三个viewController
    self.anchorVC = [[ZTAnchorViewController alloc] init];
    // 设置容器视图控制器
    [self addChildViewController:self.anchorVC];
    self.anchorVC.view.frame = CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT);
    [self.bigScrollView addSubview:self.anchorVC.view];
    
    self.listVC = [[ZTListViewController alloc] init];
    // 设置容器视图控制器
    [self addChildViewController:self.listVC];
    self.listVC.view.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    [self.bigScrollView addSubview:self.listVC.view];
    
    self.recommendVC = [[ZTRecommendViewController alloc] init];
    // 设置容器视图控制器
    [self addChildViewController:self.recommendVC];
    self.recommendVC.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self.bigScrollView addSubview:self.recommendVC.view];
    

#pragma mark - 设置首页切换下的横条
    self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(self.recommendBtn.frame.origin.x, 40, self.recommendBtn.frame.size.width, 2)];
    self.sliderView.backgroundColor = [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1];
    [self.view addSubview:self.sliderView];
    
    
}



#pragma mark - 点击按钮,改变bigScrollView的偏移量
-(void)buttonAction:(UIButton *)btn
{
    
    if (btn == self.recommendBtn) {
        
        [self.bigScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
       else if (btn == self.listBtn)
    {
        [self.bigScrollView setContentOffset:CGPointMake(WIDTH * 1, 0) animated:YES];
    }
    else if (btn == self.anchorBtn)
    {
        [self.bigScrollView setContentOffset:CGPointMake(WIDTH * 2, 0) animated:YES];
    }

}


#pragma mark - scrollView协议方法: 改变颜色
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.sliderView.frame = CGRectMake(self.bigScrollView.contentOffset.x / 3 , self.recommendBtn.frame.origin.y + self.recommendBtn.frame.size.height, self.recommendBtn.frame.size.width, 2);
    
    NSInteger num = self.bigScrollView.contentOffset.x / WIDTH;
    
    switch (num) {
            
        case 0:
            [self.recommendBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
            [self.listBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            [self.anchorBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            break;
            
        case 1:
            [self.recommendBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            [self.listBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
            [self.anchorBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            break;
            
        case 2:
            [self.recommendBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            [self.listBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
            [self.anchorBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
